﻿using BCCommon;
using BCCommon.Enums;
using BCData.Trading.Enquiry.SaleEnquiryChecklist;
using BCDto.Trading.Enquiry.SaleEnquiryChecklist;
using BCEntity.Trading.Enquiry.SaleEnquiryChecklist;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.Trading.Enquiry.SaleEnquiry;
using BCData.Trading.SaleShoppingCart;
using BCService.Trading.SaleGoods.SaleGoods;
using BCCommon.TradingEnums;

namespace BCService.Trading.Enquiry.SaleEnquiryChecklist
{
    public class SaleEnquiryChecklistService : ISaleEnquiryChecklistService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly ISaleEnquiryChecklistData saleEnquiryChecklistData;
        private readonly ISaleEnquiryData saleEnquiryData;
        private readonly ISaleShoppingCartData saleShoppingCartData;
        private readonly ISaleGoodsService saleGoodsService;
        private readonly string key = string.Format("{0}", EntityNameConst.SaleEnquiryChecklistEntity);

        public SaleEnquiryChecklistService(IDatabaseContext databaseContext,
            IRedisService redisService,
            ISaleEnquiryChecklistData saleEnquiryChecklistData,
            ISaleEnquiryData saleEnquiryData,
            ISaleShoppingCartData saleShoppingCartData,
            ISaleGoodsService saleGoodsService)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.saleEnquiryChecklistData = saleEnquiryChecklistData;
            this.saleEnquiryData = saleEnquiryData;
            this.saleShoppingCartData = saleShoppingCartData;
            this.saleGoodsService = saleGoodsService;
        }

        #region 检查销售询盘清单是否存在
        public bool Exists(long saleEnquiryChecklistId)
        {
            return this.saleEnquiryChecklistData.Exists(saleEnquiryChecklistId).GetAwaiter().GetResult();
        }
        #endregion
        
        #region 添加销售询盘清单
        public SaleEnquiryChecklistDto Add(SaleEnquiryChecklistRequestDto requestDto)
        {
            if (!this.saleEnquiryData.Exists(requestDto.SaleEnquiryId).GetAwaiter().GetResult())
            {
                throw new ArgumentException("此询盘单不存在!");
            }
            string cacheKey = this.redisService.GetSingleRootKey(key, "saleEnquiryId", requestDto.SaleEnquiryId);

            if (this.saleEnquiryChecklistData.Exists(requestDto.SaleEnquiryId, requestDto.SaleGoodsNormId).GetAwaiter().GetResult())
            {
                var checklistDto = this.saleEnquiryChecklistData.GetEntity(requestDto.SaleEnquiryId, requestDto.SaleGoodsNormId).GetAwaiter().GetResult();
                this.redisService.DeleteMultipleKey(cacheKey, false);
                return this.saleEnquiryChecklistData.IncreaseSaleCount(checklistDto.SaleEnquiryChecklistId).GetAwaiter().GetResult().As<SaleEnquiryChecklistDto>();
            }

            var saleGoodsNormDto = this.saleGoodsService.GetGoodsNorm(requestDto.SaleGoodsNormId);
            if (saleGoodsNormDto == null)
            {
                throw new ArgumentException("此商品不存在!");
            }
            
            var entity = requestDto.As<SaleEnquiryChecklistEntity>();
            entity.SaleGoodsCategoryId = saleGoodsNormDto.SaleGoodsCategoryId;
            entity.CategoryName = saleGoodsNormDto.CategoryName;
            entity.SaleGoodsId = saleGoodsNormDto.SaleGoodsId;
            entity.GoodsName = saleGoodsNormDto.GoodsName;
            entity.Sku = saleGoodsNormDto.Sku;
            entity.SaleGoodsNormId = saleGoodsNormDto.SaleGoodsNormId;
            entity.NormName = saleGoodsNormDto.NormName;
            entity.NormNo = saleGoodsNormDto.NormNo;
            entity.MainImage = saleGoodsNormDto.MainImage;
            entity.SaleUnit = saleGoodsNormDto.SaleUnit;
            entity.SaleCount = 1;
            var result = this.saleEnquiryChecklistData.Add(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(cacheKey, false);
            return result.As<SaleEnquiryChecklistDto>();
        }
        #endregion

        #region 修改销售询盘清单
        public SaleEnquiryChecklistDto Update(SaleEnquiryChecklistPutDto putDto)
        {
            if (!this.Exists(putDto.SaleEnquiryChecklistId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<SaleEnquiryChecklistEntity>();
            var dto = this.Get(putDto.SaleEnquiryChecklistId);
            string cacheKey = this.redisService.GetSingleRootKey(key, "saleEnquiryId", dto.SaleEnquiryId);

            if (putDto.ChangeNumber == ChangeNumberType.Reduce)
            {
                var result = this.saleEnquiryChecklistData.ReduceSaleCount(dto.SaleEnquiryChecklistId).GetAwaiter().GetResult().As<SaleEnquiryChecklistDto>();
                this.redisService.DeleteMultipleKey(cacheKey, false);
                return result;
            }
            else
            {
                var result = this.saleEnquiryChecklistData.IncreaseSaleCount(dto.SaleEnquiryChecklistId).GetAwaiter().GetResult().As<SaleEnquiryChecklistDto>();
                this.redisService.DeleteMultipleKey(cacheKey, false);
                return result;
            }
        }
        #endregion

        #region 删除销售询盘清单
        public bool Delete(long saleEnquiryChecklistId)
        {
            if (!this.Exists(saleEnquiryChecklistId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.saleEnquiryChecklistData.Delete(saleEnquiryChecklistId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        public bool DeleteEnquiry(long saleEnquiryId)
        {

            var result = this.saleEnquiryChecklistData.DeleteEnquiry(saleEnquiryId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 获取销售询盘清单
        public SaleEnquiryChecklistDto Get(long saleEnquiryChecklistId)
        {
            string cacheKey = this.redisService.GetKey(key, saleEnquiryChecklistId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.saleEnquiryChecklistData.GetEntity(saleEnquiryChecklistId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<SaleEnquiryChecklistDto>();
        }
        #endregion

        #region 获取销售询盘清单列表
        public IEnumerable<SaleEnquiryChecklistDto> GetList(long saleEnquiryId)
        {
            string cacheKey = this.redisService.GetKey(key, saleEnquiryId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.saleEnquiryChecklistData.GetList(saleEnquiryId).GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<SaleEnquiryChecklistDto>>();
        }
        #endregion

        #region 获取销售询盘清单分页
        public PageableList<SaleEnquiryChecklistDto> Query(long? saleEnquiryId,DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, saleEnquiryId, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.saleEnquiryChecklistData.Query(saleEnquiryId, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<SaleEnquiryChecklistDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<SaleEnquiryChecklistDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}